/* EasyWay Game Engine
 * Copyright (C) 2007 Daniele Paggi.
 *  
 * Written by: 2007 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.gui.base;

import org.easyway.interfaces.base.ITexture;
import org.easyway.interfaces.sprites.IClickable;
import org.easyway.objects.sprites2D.Mask;
import org.easyway.objects.sprites2D.Sprite;
import org.easyway.objects.text.Text;
import org.easyway.objects.texture.Texture;

public abstract class Button extends Sprite implements IClickable {

	protected static ITexture upImageDef;

	protected ITexture upImage;

	protected ITexture selectedImage;

	protected ITexture downImage;

	static {
		if (upImageDef == null) {
			upImageDef = new Texture("/org/easyway/gui/images/buttonUp.png");
		}
	}

	protected Text text;

	public Button(int x, int y, String text) {
		super(x, y, upImageDef);
		upImage = upImageDef;
		this.text = new Text(x + 10, y + 10, text, null);
		int h = this.text.getHeight();
		int w = this.text.getWidth();
		setSize(w + 20, h + 20);
		fixedOnScreen = true;
	}

	public void onClick(int x, int y) {
	}
	
	public void onOver(int nx, int ny) {
	}

	public void onDown(int x, int y) {
		if (downImage != null) {
			setRGBA(1, 1, 1, 1);
			setImage(downImage);
		} else {
			setRGBA(0, 1, 1, 1);
		}
	}

	public void onDrag(int incx, int incy) {
	}

	public void onEnter() {
		if (selectedImage != null) {
			setRGBA(1, 1, 1, 1);
			setImage(selectedImage);
		} else {
			setRGBA(1, 1, 0, 1);
		}
	}

	public void onExit() {
		// if (upImageDef != null && getImage() != upImageDef) {
		setImage(upImage);
		// }
		setRGBA(1, 1, 1, 1);
	}

	public void onRelease(int x, int y) {
		// if (upImageDef != null && getImage() != upImageDef) {
		setImage(upImage);
		// }
		setRGBA(1, 1, 1, 1);
		command();
	}

	public void setUpImage(ITexture image) {
		setImage(upImage = image);
	}

	public Mask getMask() {
		return null;
	}

	public abstract void command();

	@Override
	public void decX() {
		super.decX();
		if (text != null)
			text.setX((int) text.getX() - 1);
	}

	@Override
	public void decY() {
		super.decY();
		if (text != null)
			text.setY((int) text.getY() - 1);
	}

	@Override
	public void incX() {
		super.incX();
		if (text != null)
			text.setY((int) text.getY() + 1);
	}

	@Override
	public void incX(float incx) {
		super.incX(incx);
		if (text != null)
			text.setX((int) text.getX() + (int) incx);
	}

	@Override
	public void incY() {
		super.incY();
		if (text != null)
			text.setY((int) text.getY() + 1);
	}

	@Override
	public void incY(float incy) {
		super.incY(incy);
		if (text != null)
			text.setY((int) (text.getY() + incy));
	}

	@Override
	public void move(float incx, float incy) {
		super.move(incx, incy);
		if (text != null) {
			text.setY((int) (text.getY() + incy));
			text.setX((int) (text.getX() + incx));
		}
	}

	@Override
	public void setX(float x) {
		super.setX(x);
		if (text != null)
			text.setX((int) x + 10);
	}

	@Override
	public void setXY(float x, float y) {
		super.setXY(x, y);
		if (text != null) {
			text.setX((int) x + 10);
			text.setY((int) y + 10);
		}
	}

	@Override
	public void setY(float y) {
		super.setY(y);
		if (text != null)
			text.setY((int) y + 10);
	}
	
	public void setImage( ITexture image ) {
		this.image = image;
	}
	
	public void destroy() {
		super.destroy();
		text.destroy();
	}

}
